const app = getApp()
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import api from '../../utils/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabbarlist:[
      {
        id:1,
        name:'综合排名',
        selected:true,
        type:1
      },
      {
        id:3,
        name:'最活跃',
        selected:false,
        type:1
      },
      {
        id:5,
        name:'最人气',
        selected:false,
        type:1
      }
    ],
    tabType:1,//1显示全国-2不显示
    regionPop:false,
    regionText:'全国',
    topList:[],//排行123
    otherList:[],//其他排行
    citycode:'',//
    status:1,//根据status去判断-全国/本市 的分类//状态，1综合排名全国，2综合排名本市，3最活跃全国，4最活跃本市，5最人气全国，6最人气本市
    
  },
  
  navYouthDetail(e){
    wx.navigateTo({
      url: '/pages/index_youthhome_detail/index?youthid=' + e.currentTarget.dataset.id,
    })
  },
  //全国-本市-显示
  regionPopShow(){
    this.setData({
      regionPop:!this.data.regionPop
    })
  },
  //选择--全国-本市
  regionSelected(e){
    let regionText = e.currentTarget.dataset.text;
    let regionItemId = e.currentTarget.dataset.id;
    this.setData({
      regionText
    })
    if(this.data.status == 1 && regionItemId == 0){
      this.getChart(this.data.citycode,1)
    }else if(this.data.status == 1 && regionItemId == 1){
      this.getChart(this.data.citycode,2)
    }else if(this.data.status == 3 && regionItemId == 0){
      this.getChart(this.data.citycode,3)
    }else if(this.data.status == 3 && regionItemId == 1){
      this.getChart(this.data.citycode,4)
    }else if(this.data.status == 5 && regionItemId == 0){
      this.getChart(this.data.citycode,5)
    }else if(this.data.status == 5 && regionItemId == 1){
      this.getChart(this.data.citycode,6)
    }
  },
  //tab切换
  selected(e){
    let type = e.currentTarget.dataset.type;
    let ind = e.currentTarget.dataset.id;
    let status = e.currentTarget.dataset.status;
    //默认选中
    this.data.tabbarlist.forEach((item,index) => {
      index == ind ? item.selected = true :item.selected = false;
    })
    this.setData({
      tabbarlist:this.data.tabbarlist,
      tabType:type,
      status:status,
      regionText:'全国'
    })
    //type :1,请求综合排名，最活跃，最人气
    //type :2,其他分类
    if(type == 1){
      this.getChart(this.data.citycode,status)
    }else{
      this.getOther(status)
    }
  },
  onClickLeft(){
    //返回上一级关闭当前页面
    wx.navigateBack({
      delta: 1
    })
  },
  //其他分类
  async getOther(id){
    let data={}
    data['flag'] = 'ranking_else_list'
    data['citycode'] =app.globalData.firstAreaId
    data['rank_id'] = id
    
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      let topList = result.slice(0,3)
      let otherList = result.slice(3)
      this.setData({
        topList,
        otherList
      }) 
      
    }else {
      Toast(message)
    }
  },
  //顶部tab
  async getTabs(){
    let data={}
    data['flag'] = 'ranking_else'
    
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      let tabs = [];
      result.forEach((item,index) => {
          tabs.push({
            id:item.id,
            name:item.title,
            selected:false,
            type:2
          })
      })
      this.data.tabbarlist = this.data.tabbarlist.concat(tabs) 
      this.setData({
        tabbarlist:this.data.tabbarlist
      })
    }else {
      Toast(message)
    }
  },
  //排行榜
  async getChart(citycode,status){
    let data={}
    data['flag'] = 'ranking_list'
    data['citycode'] =app.globalData.firstAreaId
    data['cityName'] = app.globalData.address.city
    data['status'] = status
   
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      console.log(result)
      result.forEach((item,index) => {
        item.imgurl[0] = this.loadImg(item.imgurl[0])
      })
      let topList = result.slice(0,3)
      let otherList = result.slice(3)
      this.setData({
        topList,
        otherList
      }) 
    }else {
      this.setData({
        topList:[],
        otherList:[]
      }) 
      // Toast(message)
    }

  },
   //图片加载
   loadImg(img){
    let url =  api.checkurl(img)
    return url
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      citycode:app.globalData.address.city_level,
    })
    this.getChart(this.data.citycode,this.data.status)
    this.getTabs()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})